﻿using EPiServer.Common;
using EPiServer.Community.Blog;
using EPiServer.Community.Calendar;
using EPiServer.Community.Club;
using EPiServer.Community.Forum;
using EPiServer.Community.Poll;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;

namespace OpenWaves.ActivityFeed.EPiServer.Community
{
    [InitializableModule]
    public class ActivityFeedCommunityIntegrationModule : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            ClubHandler.Instance.MembershipAdded += this.OnClubMembershipAdded;
            ClubHandler.Instance.PollAdded += this.OnClubPollAdded;
            CalendarHandler.Instance.EventAdded += this.OnEventAdded;
            BlogHandler.Instance.EntryAdded += this.OnBlogEntryAdded;
            BlogHandler.Instance.EntryCommentAdded += this.OnBlogEntryCommentAdded;
            ForumHandler.Instance.TopicAdded += this.OnForumTopicAdded;
        }

        private void OnForumTopicAdded(string sender, EPiServerCommonEventArgs e)
        {
            var topic = e.Object as global::EPiServer.Community.Forum.Topic;
            if (topic == null)
                return;

            var connector = ServiceLocator.Resolve<IActivityFeedEPiServerCommunityConnector>();
            connector.OnTopicAdded(topic);
        }

        private void OnClubPollAdded(string sender, EPiServerCommonEventArgs e)
        {
            var poll = e.Object as Poll;
            if (poll == null)
                return;

            var connector = ServiceLocator.Resolve<IActivityFeedEPiServerCommunityConnector>();
            connector.OnPollAdded(poll);
        }

        private void OnClubMembershipAdded(string sender, EPiServerCommonEventArgs e)
        {
            var membership = e.Object as Membership;
            if (membership == null)
                return;

            var connector = ServiceLocator.Resolve<IActivityFeedEPiServerCommunityConnector>();
            connector.OnClubMembershipAdded(membership);            
        }

        private void OnBlogEntryCommentAdded(string sender, EPiServerCommonEventArgs e)
        {
            var entryComment = e.Object as EntryComment;
            if (entryComment == null)
                return;

            var connector = ServiceLocator.Resolve<IActivityFeedEPiServerCommunityConnector>();
            connector.OnBlogEntryCommentAdded(entryComment);                        
        }

        private void OnBlogEntryAdded(string sender, EPiServerCommonEventArgs e)
        {
            var entry = e.Object as Entry;
            if (entry == null)
                return;

            var connector = ServiceLocator.Resolve<IActivityFeedEPiServerCommunityConnector>();
            connector.OnBlogEntryAdded(entry);                                    
        }

        void OnEventAdded(string sender, EPiServerCommonEventArgs e)
        {
            var calendarEvent = e.Object as Event;
            if (calendarEvent == null)
                return;

            var connector = ServiceLocator.Resolve<IActivityFeedEPiServerCommunityConnector>();
            connector.OnCalendarEventAdded(calendarEvent);
        }

        public void Uninitialize(InitializationEngine context)
        {
        }

        public void Preload(string[] parameters)
        {
        }
    }
}